# -*- coding: utf-8 -*-
'''
Created on Apr 3, 2012

@author: LONG HOANG GIANG
'''

import os, sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import simplejson as json
import gzip
import datetime
from CrawlerLib.http import Http

FILE_DATA = 'nuhoangaicap.data'

def loadData(fileName):
    f = gzip.open(fileName, 'rb')
    content = f.read()
    f.close()
    data = json.loads(content)
    return data;

def saveData(fileName, data):
    content = json.dumps(data)
    f = gzip.open(fileName, 'wb')
    f.write(content)
    f.close()

def pushFirst(item):
    data = loadData(FILE_DATA)
    n = len(data)
    data.append(None)
    for i in range(n, 0, -1):
        data[i] = data[i-1]
    data[0] = item;
    saveData(FILE_DATA, data)

def pushLast(item):
    data = loadData(FILE_DATA)
    data.append(item)
    saveData(FILE_DATA, data)

def getImagesOfChapter(url, chapterName):
    
    tree = Http.getXMLTree(url)
    xpathNode = "//div[@class='entry']//img"
    for item in tree.xpath(xpathNode):
        print item.get('src')
    
def update(chapterName):
    
    images = '''http://3.bp.blogspot.com/-GrINTxO7Q6c/T4K4l_QlM1I/AAAAAAABqNo/lzj95m3pZso/s1200/00.jpg
http://4.bp.blogspot.com/-MN61KhfaX0M/T4K4ovCoCbI/AAAAAAABqN8/ssD-M4gKd88/s1200/000000.jpg
http://4.bp.blogspot.com/-9dtJ13kPqt0/T4K4lWjSQ2I/AAAAAAABqNk/CriAgEapHag/s1200/01.jpg
http://3.bp.blogspot.com/-1mDZmUipRu4/T4K4n2YZehI/AAAAAAABqN4/e4JghgXrJ9M/s1200/02.jpg
http://4.bp.blogspot.com/-cvNWzwWqT8g/T4K4ppTszgI/AAAAAAABqOE/VhP23Dcys_w/s1200/03.jpg
http://3.bp.blogspot.com/-_NSwBvgWWw4/T4K4qLZQkMI/AAAAAAABqOI/D3Fou10C0IQ/s1200/04.jpg
http://4.bp.blogspot.com/-T4pHSsKHhrM/T4K4q4aMArI/AAAAAAABqOQ/pxPh4A3lLKU/s1200/05.jpg
http://3.bp.blogspot.com/-Lo7ve-Qgta8/T4K4sG14dLI/AAAAAAABqOg/rgihjEToYqw/s1200/06.jpg
http://4.bp.blogspot.com/-otlauFgEyGg/T4K4sr-PlII/AAAAAAABqOk/fEuJKA0cfQE/s1200/07.jpg
http://3.bp.blogspot.com/-twaKVE467jc/T4K4u1FpsMI/AAAAAAABqOw/hmO9eu-h7KI/s1200/08.jpg
http://4.bp.blogspot.com/-5LmoU-u6fuw/T4K4uTseqRI/AAAAAAABqO4/38Gts5YU2sU/s1200/09.jpg
http://4.bp.blogspot.com/-6jCFC2QwEuY/T4K4vKjd7tI/AAAAAAABqO0/HuYPbjzzdEw/s1200/10.jpg
http://4.bp.blogspot.com/-1f62FvwZyQw/T4K4x5788rI/AAAAAAABqPI/A9i4yTtVH9g/s1200/11.jpg
http://4.bp.blogspot.com/-X-zQ8UhrF3Y/T4K4yV-VSZI/AAAAAAABqPQ/VU25Oo_JnHA/s1200/12.jpg
http://3.bp.blogspot.com/-u1BNOw2Ctik/T4K4yav_mpI/AAAAAAABqPM/1SZwiACIXls/s1200/13.jpg
http://4.bp.blogspot.com/-tbvW9bzYoFA/T4K4z4SPXUI/AAAAAAABqPc/gDYwPywyVA8/s1200/14.jpg
http://4.bp.blogspot.com/-qbnzpZTMXgY/T4K40MlpHZI/AAAAAAABqPo/KsE7y6mR4xU/s1200/15.jpg
http://4.bp.blogspot.com/-gPaVI1Q5TAw/T4K40GLOCPI/AAAAAAABqPs/1O7RaSPoUN4/s1200/16.jpg
http://4.bp.blogspot.com/-xgqf8WhhaDQ/T4K41TXJ_0I/AAAAAAABqPw/ZTVkb3g94cg/s1200/17.jpg
http://4.bp.blogspot.com/-jccG7TKqGXA/T4K42MPbHhI/AAAAAAABqP4/fAG6iM60Wyc/s1200/18.jpg
http://4.bp.blogspot.com/-oZ745Anl1s0/T4K43LNJweI/AAAAAAABqQI/MVsNU0gvIgE/s1200/19.jpg
http://4.bp.blogspot.com/-9Hn4-lmWNzk/T4K43GEBN0I/AAAAAAABqQQ/1rNfN5kzIpQ/s1200/20.jpg
http://4.bp.blogspot.com/-ipJUJvDfozY/T4K43qrMBpI/AAAAAAABqQM/s5LPLKTOjBQ/s1200/21.jpg
http://4.bp.blogspot.com/-Tgm-iglxe-Q/T4K45kPsqrI/AAAAAAABqQg/gwejeCvvORg/s1200/22.jpg
http://4.bp.blogspot.com/-65eGY05yXo8/T4K46nDjN-I/AAAAAAABqQo/tRtyXUtGNUo/s1200/23.jpg
http://3.bp.blogspot.com/-0PrhvVJmmZk/T4K46gnW_4I/AAAAAAABqQk/dLt_1VnIiok/s1200/24.jpg
http://4.bp.blogspot.com/-t8T20u_258E/T4K48Lp6VJI/AAAAAAABqQ8/nIyNS9xg-wM/s1200/25.jpg
http://4.bp.blogspot.com/-5qHLvjpmXlk/T4K48gBfq-I/AAAAAAABqQ4/9gO21KyxLWA/s1200/26.jpg
http://4.bp.blogspot.com/-nh0oj30KcMo/T4K49lY4N5I/AAAAAAABqRA/_tgn8rMWxhs/s1200/27.jpg
http://4.bp.blogspot.com/-zcoaB3GlBlo/T4K5Bkw_kzI/AAAAAAABqRk/DU-z0LPWcd4/s1200/28.jpg
http://4.bp.blogspot.com/-8R-4bZYTUqo/T4K5AFD_bRI/AAAAAAABqRY/Lx1rOGUMEPg/s1200/29.jpg
http://3.bp.blogspot.com/-G76JloPQp2Y/T4K4_gaqKlI/AAAAAAABqRQ/97lzFxwzecE/s1200/30.jpg
http://3.bp.blogspot.com/-XGwEv6r8XF0/T4K5Bx2x_ZI/AAAAAAABqRo/IK_y0fQ9bDU/s1200/31.jpg'''
    
    images = images.strip().split("\n")
    item = {'chapter': chapterName, 'images': images}
    pushFirst(item)
        
if __name__ == '__main__':
    
#    getImagesOfChapter('http://doctruyen.vechai.info/nu-hoang-ai-cap-chap-19/', 'Nữ Hoàng Ai Cập chap 19')
    update('Nữ Hoàng Ai Cập chap 19')
    
    print('>>Finished at {0}'.format(datetime.datetime.now()))
    os._exit(1)

    